package com.xbongbong.pro.statistic.help;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.constant.BiConstant;
import com.xbongbong.paas.domain.entity.ext.StatisticPaasFormDataEsEntity;
import com.xbongbong.paas.elasticsearch.constant.EsNormalConstant;
import com.xbongbong.paas.elasticsearch.pojo.StatisticBiFormDataEntity;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.rabbitmq.binders.BiProducerBinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 消息发送
 * @author zhouwq
 * @date 2019/5/10 9:59
 */
@Component
@EnableBinding(BiProducerBinder.class)
public class InitMessageQueueHelp {
    private static final Logger LOG = LoggerFactory.getLogger(BiProducerBinder.class);


    @Resource
    private BiProducerBinder biProducerBinder;

    /**
     * 反射获取方法名 发送信息至MQ
     * @param message 发送消息主体
     * @param index 索引名
     */
    public void initMessage(List<StatisticPaasFormDataEsEntity> message, String index){
        String esName = ProBaseConfig.getEsName();
        index  = index.replace(esName,"");
        for (int i = 0; i < message.size(); i += EsNormalConstant.MQ_NUM) {
            int toIndex = i +  EsNormalConstant.MQ_NUM;
            if(toIndex > message.size() ){
                toIndex = message.size() ;
            }
            List<StatisticPaasFormDataEsEntity> newList = message.subList(i, toIndex);
            if(!newList.isEmpty()) {
                String str = JSONArray.toJSONString(newList, SerializerFeature.DisableCircularReferenceDetect);
                try {
                    Method method = biProducerBinder.getClass().getMethod(index);
                    Object obj = method.invoke(biProducerBinder);
                    MessageChannel messageChannel = (MessageChannel) obj;
                    Message msg = MessageBuilder.withPayload(str).build();
                    boolean sendStatus = messageChannel.send(msg);
                    LOG.info("发送索引"+ index +"消息状态：   " + sendStatus);
                } catch (Exception e) {
                    LOG.error("发送索引"+ index +"消息失败",e);
                }
            }
        }
    }

    public void initBiMessage(List<StatisticBiFormDataEntity> message, String index){
        String esName = ProBaseConfig.getEsName();
        index  = index.replace(esName,"");
        index  = index.replace(BiConstant.INDEX_SUFFIX,"");
        for (int i = 0; i < message.size(); i += EsNormalConstant.MQ_NUM) {
            int toIndex = i +  EsNormalConstant.MQ_NUM;
            if(toIndex > message.size() ){
                toIndex = message.size() ;
            }
            List<StatisticBiFormDataEntity> newList = message.subList(i, toIndex);
            if(!newList.isEmpty()) {
                String str = JSONArray.toJSONString(newList, SerializerFeature.DisableCircularReferenceDetect);
                try {
                    Method method = biProducerBinder.getClass().getMethod(index);
                    Object obj = method.invoke(biProducerBinder);
                    MessageChannel messageChannel = (MessageChannel) obj;
                    Message msg = MessageBuilder.withPayload(str).build();
                    boolean sendStatus = messageChannel.send(msg);
                    LOG.info("发送索引"+ index +"消息状态：   " + sendStatus);
                } catch (Exception e) {
                    LOG.error("发送索引"+ index +"消息失败",e);
                }
            }
        }
    }

}
